home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 151-175 / disk_166 / stevie / source / fileio.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  209 lines

  1. /*
  2.  * STEVIE - Simply Try this Editor for VI Enthusiasts
  3.  *
  4.  * Code Contributions By : Tim Thompson           twitch!tjt
  5.  *                         Tony Andrews           onecom!wldrdg!tony 
  6.  *                         G. R. (Fred) Walter    watmath!watcgl!grwalter 
  7.  */
  8.  
  9. #include "stevie.h"
  10.  
  11. void
  12. filemess(s)
  13.     char           *s;
  14. {
  15.     sprintf(IObuff, "\"%s\" %s", ((Filename == NULL) ? "" : Filename), s);
  16.     msg(IObuff);
  17. }
  18.  
  19. void
  20. renum()
  21. {
  22.     LPTR           *p;
  23.     unsigned int    l = 0;
  24.  
  25.     for (p = Filemem; p != NULL; p = nextline(p), l += LINEINC)
  26.     p->linep->num = l;
  27.  
  28.     Fileend->linep->num = 0xffff;
  29. }
  30.  
  31. #ifdef    MEGAMAX
  32. overlay "fileio"
  33. #endif
  34.  
  35. bool_t
  36. readfile(fname, fromp, nochangename)
  37.     char           *fname;
  38.     LPTR           *fromp;
  39.     bool_t          nochangename;    /* if TRUE, don't change the Filename */
  40. {
  41.     FILE           *f, *fopen();
  42.     LINE           *curr;
  43.     char           *p;
  44.     int             i, c;
  45.     long            nchars;
  46.     int             unprint = 0;
  47.     int             linecnt = 0;
  48.     bool_t          wasempty = bufempty();
  49.  
  50.     curr = fromp->linep;
  51.  
  52.     if (!nochangename)
  53.     Filename = strsave(fname);
  54.  
  55.     if ((f = fopen(fname, "r")) == NULL)
  56.     return TRUE;
  57.  
  58.     filemess("");
  59.  
  60.     i = 0;
  61.     for (nchars = 0; (c = getc(f)) != EOF; nchars++) {
  62.     if (c >= 0x80) {
  63.         c -= 0x80;
  64.         unprint++;
  65.     }
  66.     /*
  67.      * Nulls are special, so they can't show up in the file. We should
  68.      * count nulls seperate from other nasties, but this is okay for now. 
  69.      */
  70.     if (c == NUL) {
  71.         unprint++;
  72.         continue;
  73.     }
  74.     if (c == '\n') {    /* process the completed line */
  75.         int             len;
  76.         LINE           *lp;
  77.  
  78.         IObuff[i] = NUL;
  79.         len = strlen(IObuff) + 1;
  80.         if ((lp = newline(len)) == NULL) {
  81.         fprintf(stderr, "not enough memory - should never happen");
  82.         getout(1);
  83.         }
  84.         strcpy(lp->s, IObuff);
  85.  
  86.         curr->next->prev = lp;    /* new line to next one */
  87.         lp->next = curr->next;
  88.  
  89.         curr->next = lp;    /* new line to prior one */
  90.         lp->prev = curr;
  91.  
  92.         curr = lp;        /* new line becomes current */
  93.         i = 0;
  94.         linecnt++;
  95.     } else
  96.         IObuff[i++] = (char) c;
  97.     }
  98.     fclose(f);
  99.  
  100.     /*
  101.      * If the buffer was empty when we started, we have to go back and remove
  102.      * the "dummy" line at Filemem and patch up the ptrs. 
  103.      */
  104.     if (wasempty) {
  105.     LINE           *dummy = Filemem->linep;    /* dummy line ptr */
  106.  
  107.     free(dummy->s);        /* free string space */
  108.     Filemem->linep = Filemem->linep->next;
  109.     free((char *) dummy);    /* free LINE struct */
  110.     Filemem->linep->prev = NULL;
  111.  
  112.     Curschar->linep = Filemem->linep;
  113.     Topchar->linep = Filemem->linep;
  114.     }
  115.     if (unprint > 0)
  116.     p = "\"%s\" %d lines, %ld characters (%d un-printable))";
  117.     else
  118.     p = "\"%s\" %d lines, %ld characters";
  119.  
  120.     sprintf(IObuff, p, fname, linecnt, nchars, unprint);
  121.     msg(IObuff);
  122.     renum();
  123.     return FALSE;
  124. }
  125.  
  126.  
  127. /*
  128.  * writeit - write to file 'fname' lines 'start' through 'end' 
  129.  *
  130.  * If either 'start' or 'end' contain null line pointers, the default is to use
  131.  * the start or end of the file respectively. 
  132.  */
  133. bool_t
  134. writeit(fname, start, end)
  135.     char           *fname;
  136.     LPTR           *start, *end;
  137. {
  138.     FILE           *f, *fopen();
  139.     FILE           *fopenb();    /* open in binary mode, where needed */
  140.     char            backup[16], *s;
  141.     long            nchars;
  142.     int             lines;
  143.     LPTR           *p;
  144.  
  145.     sprintf(IObuff, "\"%s\"", fname);
  146.     msg(IObuff);
  147.  
  148.     /*
  149.      * Form the backup file name - change foo.* to foo.bak 
  150.      */
  151.     strcpy(backup, fname);
  152.     for (s = backup; *s && *s != '.'; s++);
  153.     *s = NUL;
  154.     strcat(backup, ".bak");
  155.  
  156.     /*
  157.      * Delete any existing backup and move the current version to the backup.
  158.      * For safety, we don't remove the backup until the write has finished
  159.      * successfully. And if the 'backup' option is set, leave it around. 
  160.      */
  161.     rename(fname, backup);
  162.  
  163.  
  164.     f = P(P_CR) ? fopen(fname, "w") : fopenb(fname, "w");
  165.  
  166.     if (f == NULL) {
  167.     emsg("Can't open file for writing!");
  168.     return FALSE;
  169.     }
  170.     /*
  171.      * If we were given a bound, start there. Otherwise just start at the
  172.      * beginning of the file. 
  173.      */
  174.     if (start == NULL || start->linep == NULL)
  175.     p = Filemem;
  176.     else
  177.     p = start;
  178.  
  179.     lines = 0;
  180.     nchars = 0;
  181.     do {
  182.     fprintf(f, "%s\n", p->linep->s);
  183.     nchars += strlen(p->linep->s) + 1;
  184.     lines++;
  185.  
  186.     /*
  187.      * If we were given an upper bound, and we just did that line, then
  188.      * bag it now. 
  189.      */
  190.     if (end != NULL && end->linep != NULL) {
  191.         if (end->linep == p->linep)
  192.         break;
  193.     }
  194.     } while ((p = nextline(p)) != NULL);
  195.  
  196.     fclose(f);
  197.     sprintf(IObuff, "\"%s\" %d lines, %ld characters", fname, lines, nchars);
  198.     msg(IObuff);
  199.     UNCHANGED;
  200.  
  201.     /*
  202.      * Remove the backup unless they want it left around 
  203.      */
  204.     if (!P(P_BK))
  205.     remove(backup);
  206.  
  207.     return TRUE;
  208. }
  209.